import { Injectable } from '@angular/core';
import { CompanyModel } from './company/company.model';

export enum ResearchType {
    risk,
    esg,
    governance,
    controversy,
    gccs,
    compliance
}

export enum ResearchState {
    FrameworkNotApplicable = 0,
    ResearchInProgress = 1,
    ResearchAvailable = 2,
    ResearchNotAvailable = 3
}


/**
 * Research service
 * 
 * Author: Sorin Spiru
 * Date: 22.02.2018
 */
@Injectable()
export class ResearchService {

    /**
     * Checks if company has research product in desired status
     * @param researchType Research product
     * @param company Company data
     * @param status Research status
     */
    public hasResearch(researchType: ResearchType, company: CompanyModel, status: ResearchState): boolean {
        let result: boolean = false;
        switch (status) {
            case ResearchState.ResearchInProgress:
                result = this.hasResearchInStatus(researchType, company, 1)
                break;
            case ResearchState.ResearchAvailable:
                result = this.hasResearchInStatus(researchType, company, 2)
                break;
            case ResearchState.ResearchNotAvailable:
                result = (this.hasResearchInStatus(researchType, company, 0) ||
                    this.hasResearchInStatus(researchType, company, 3));
                break;
        }

        return result;
    }

    /*
    1 - in progress
    2 - available
    0,3 - not available
    */
    public hasResearchInStatus(researchType: ResearchType, company: CompanyModel, statusCode: number): boolean {
        let result: boolean = false;

        switch (researchType) {
            case ResearchType.esg:
                //company is either core or comprehesive. default value set to 3 for unavailable research
                result = (company.coreEsgResearchState === statusCode && company.comprehensiveEsgResearchState === 3) || 
                (company.coreEsgResearchState === 3 && company.comprehensiveEsgResearchState === statusCode);
                break;
            case ResearchType.governance:
                result = company.corporateGovernanceResearchState === statusCode;
                break;
            case ResearchType.controversy:
                result = company.controversyResearchState === statusCode;
                break;
            case ResearchType.gccs:
                result = company.gccsResearchState === statusCode;
                break;
            case ResearchType.compliance:
                result = company.productInvolvementResearchState === statusCode;
                break;
            case ResearchType.risk:
                result = company.riskRatingResearchState === statusCode;
                break;
        }

        return result;
    }
}